import McMv from "./McMv";
import McLoad from "./McLoad";

    export class MovieClip {
        private static _mcArr: object = {};
        private static _handNum: object = {};

        public static get mcArr() {
            return this._mcArr;
        }
        public static get handNum() {
            return this._handNum;
        }

        /**
         * 动画尽量都从这里取出来 请操作拷贝的数据  默认锚点0.5 0.5
         * @param type 一般指的包名
         * @param callBack 回调new出的动画
         * @param userClass 绑定的类  McCom的子类
         */
        static getMcByType<T extends McMv>(type: string, complete: Laya.Handler, userClass: new () => T): McLoad {
            MovieClip._mcArr[type] || (MovieClip._mcArr[type] = new McLoad(type));
            (MovieClip._mcArr[type] as McLoad).getMcCom(complete, userClass);
            MovieClip._handNum[type] ? MovieClip._handNum[type]++ : MovieClip._handNum[type] = 1;
            return MovieClip._mcArr[type];
        }

        static loadMcRes(type: string, complete?: Laya.Handler) {
            MovieClip._mcArr[type] || (MovieClip._mcArr[type] = new McLoad(type));
            (MovieClip._mcArr[type] as McLoad).loadRes(complete);
        }

        //增加一次引用
        static addMcNode(type: string) {
            MovieClip._mcArr[type] || (MovieClip._mcArr[type] = new McLoad(type));
            MovieClip._handNum[type] ? MovieClip._handNum[type]++ : MovieClip._handNum[type] = 1;
        }

        //清除此类特效
        static clearAllMcByType(type: string) {
            let num = MovieClip._handNum[type];
            if (num != null) {
                delete MovieClip._handNum[type];
                (this._mcArr[type] as McLoad).clearMcAndCom();
                delete MovieClip._mcArr[type];
            }
        }

        //清理同类特效引用 次数
        static clearMcTimesByType(type: string, times: number = 1) {
            let num = MovieClip._handNum[type];
            if (num != null) {
                MovieClip._handNum[type] = num - times;
                if (MovieClip._handNum[type] <= 0) {
                    if (num > 0) {
                        (this._mcArr[type] as McLoad).clearMcAndCom();
                    }
                    if (MovieClip._handNum[type] <= 0) {
                        delete MovieClip._mcArr[type];
                        delete MovieClip._handNum[type];
                    }
                }
            }
        }
    }
